<?php
namespace app\platform\controller\terminal;
use app\BaseController;
use app\common\logic\DataLogic;
use app\common\model\account\AccountModel;
use app\common\model\account\ShopAccountModel;

class ShopAccount extends BaseController{
    public function index()
    {
        $where=[
            ['shop_id','=',$this->request->shopId]
        ];
        if($this->request->role_id == 2) $where[]=['role_id','=',2];
        $shop_account= new ShopAccountModel();
        $order=['add_time'=>'desc'];
        $field='shop_account_id,account_id,add_time';
        $append=['add_time_format'];
        $shop_account=$shop_account->with(['account'=>function($query){
            $query->field('account_id,username,mobile');
        }]);
        $return=DataLogic::getDataList($shop_account,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function show()
    {
        $shop_account_id = $this->request->param('shop_account_id/d',0);
        if($shop_account_id==0){
            return $this->success([]);
        }

        if(null === $shop_account = ShopAccountModel::find($shop_account_id)){
            return $this->error('帐号不见了');
        }
        if($shop_account->shop_id != $this->request->shopId){
            return $this->error('这个商铺没有这个账号');
        }
        if($this->request->role_id==2 && $shop_account->role_id==1){
            return $this->error('您没有权限修改这个账号');
        }
        if(null === $account = AccountModel::find($shop_account->account_id)){
            return $this->error('账号不存在');
        }
        if($account->is_delete != 0){
            return $this->error('账号不存在');
        }
        $detail=[
            'username'=>$account->username,
            'mobile'=>$account->mobile
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $username=$this->request->param('username');
        $mobile=$this->request->param('mobile');
        if(empty($username) || mb_strlen($username) >20){
            return $this->error('用户名需要填写且长度不能大于20');
        }
        if(empty($mobile) || !is_mobile($mobile)){
            return $this->error('请填写正确的手机号');
        }
       
        if(null === $account = AccountModel::where([['mobile','=',$mobile],['is_delete','=',0]])->find()){
            $account = new AccountModel();
            $account->mobile=$mobile;
            $account->username=$username;
            $account->password='e10adc3949ba59abbe56e057f20f883e';
            $account->number=date("YmdHis").rand(111111,999999);
            $account->save();
        }
        
        if(null !== ShopAccountModel::where([
                ['shop_id','=',$this->request->shopId],
                ['account_id','=',$account->account_id]
            ])->find()){
            return $this->error('这个帐号已经与这个商铺绑定过了');
        }
     
        $shop_account=new ShopAccountModel();
        $shop_account->shop_id=$this->request->shopId;
        $shop_account->role_id=2;
        $shop_account->account_id=$account->account_id;
        $shop_account->save();
        return $this->success('操作成功');
    }

    public function del()
    {
        $shop_account_id = $this->request->param('shop_account_id/d',0);

        if(null === $shop_account = ShopAccountModel::find($shop_account_id)){
            return $this->error('帐号不见了');
        }
        if($shop_account->shop_id != $this->request->shopId){
            return $this->error('这个商铺没有这个账号');
        }
        if($shop_account->role_id==1){
            return $this->error('创始人无法解绑');
        }
        if($this->request->role_id==2 && $shop_account->role_id==1){
            return $this->error('您没有权限解绑这个账号');
        }
        if(null === $account = AccountModel::find($shop_account->account_id)){
            return $this->error('账号不存在');
        }
        if($account->is_delete != 0){
            return $this->error('账号不存在');
        }
        $shop_account->delete();
        return $this->success('解绑成功');
    }
}
